home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Software Vault: The Gold Collection
/
Software Vault - The Gold Collection (American Databankers) (1993).ISO
/
cdr53
/
pctv4n_1.zip
/
SQLFUNCS.C
< prev
next >
Wrap
Text File
|
1993-06-10
|
7KB
|
333 lines
/**************************************************
* FILE NAME: SQLfuncs.c TITLE: SQL database API
*
* AUTHOR: K. E. North II
*
* SYNOPSIS:
*
* application interface to Pioneer's Q+E Lib
*
***************************************************/
#undef DEBUG
#define __QEDBF
#include <windows.h>
#include <stdio.h>
#include <dos.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include <alloc.h>
#ifndef __QEDEFS_H
/*QELib-specific definitions */
#include "QEdefs.h"
#endif
#ifndef __QEAPI_H
/* function prototypes for QELib calls */
#include "QEapi.h"
#endif
#ifndef __QCOLUMN_H
/* QELib column description */
#include "qcolumn.h"
#endif
#ifndef __DBTYPES_H
#include "dbtypes.h"
#endif
#ifndef __REQUEST_H
#include "request.h"
#endif
#ifndef __SQLFUNCS_H
/* portable SQL API functions */
#include "sqlfuncs.h"
#endif
/**************************************************
* FUNCTION NAME: dbClose
*
* SYNOPSIS:
*
* close a data base session - terminate connection
*
***************************************************/
int dbClose(CONNECTION si)
{
int ExitStatus;
/* to logout */
ExitStatus = dbDisconnect( si );
if (ExitStatus)
return ExitStatus;
else
return SQL_SUCCESS;
}
/**************************************************
* FUNCTION NAME: dbRequest
*
* SYNOPSIS:
*
* prep/exec an SQL statement
* for Q+E Lib
*
***************************************************/
int dbRequest( REQUEST rq, char *statement,
CONNECTION si )
{
HSTMT hstmt;
POINTER st;
int StatementLength;
/* get length of */
/* SQL command string */
StatementLength = strlen (statement);
if (StatementLength > MAXLEN)
{
rq->ExceedsMax=TRUE;
}
else rq->ExceedsMax = FALSE;
/* copy 1st SQL string into request */
strcpy(rq->Statement,statement);
/* build FAR * SQL request */
st = BuildRequest( rq );
rq->stptr = st;
hstmt = ExecuteStatement( st, si );
/* if there is an error, hstmt is a 0 */
/* or negative number */
if (hstmt <= NOHANDLE)
{
return REQUEST_ERROR;
}
rq->hstmt = hstmt; /* save request handle */
return SQL_SUCCESS;
}
/**************************************************
* FUNCTION NAME: BuildRequest
*
* SYNOPSIS: prepare an SQL request
***************************************************/
POINTER BuildRequest( REQUEST rq )
{
POINTER stptr;
stptr = (char far *) farmalloc(MAXSQL);
movedata
(FP_SEG(rq->Statement), FP_OFF(rq->Statement),
FP_SEG(stptr), FP_OFF(stptr),
1+strlen(rq->Statement));
return stptr;
}
/**************************************************
* FUNCTION NAME: ExecuteStatement
*
* SYNOPSIS: executes a direct or prepared request
**************************************************/
HSTMT ExecuteStatement( POINTER stptr,
CONNECTION si )
{
HSTMT hstmt;
int Status;
hstmt = qeExecSQL (si->hdbc, stptr);
if (hstmt == 0)
{
Status = qeErr();
return Status;
}
return hstmt;
}
/**************************************************
* FUNCTION NAME: dbDescribe
*
* SYNOPSIS:
*
*
***************************************************/
int dbDescribe(int ColumnPos,
struct ColumnDesc *col,
REQUEST rq)
{
char far *name;
int Status;
col->namelen = NAMELENGTH;
col->data_type = 0;
col->length = 0;
col->scale = 0;
strcpy(col->fldname," ");
/* get the column name, type, width, etc. */
name = qeColName ( rq->hstmt, ColumnPos );
Status = qeErr();
if (Status == SQL_SUCCESS)
{
strcpy(col->fldname,name);
}
else return Status;
/* get the column type */
col->data_type = qeColType ( rq->hstmt,
ColumnPos );
Status = qeErr();
if (Status != SQL_SUCCESS)
return Status;
/* get the column width */
col->length = qeColWidth ( rq->hstmt,
ColumnPos );
Status = qeErr();
if (Status == SQL_SUCCESS)
{
if (col->data_type == CHARACTER)
--col->length;
}
else return Status;
if (col->data_type == DECIMAL)
{
col->scale = qeColScale ( rq->hstmt,
ColumnPos );
Status = qeErr();
if (Status != SQL_SUCCESS)
return Status;
}
return SQL_SUCCESS;
}
/**************************************************
* FUNCTION NAME: dbConnect
*
* SYNOPSIS:
* open an database connection
***************************************************/
int dbConnect( CONNECTION si, DATASRC dsrc)
{
int Status;
/********************************/
/* Connect to Q+E database */
/********************************/
si->cstr = BuildConnectString(dsrc);
/* QELib connect */
si->hdbc = qeConnect (si->cstr);
Status = qeErr();
if (si->hdbc == 0)
{
#ifdef DEBUG
printf ("<Connect> qeConnect failure,
Status: %d\n", Status);
#endif
return Status;
};
return SQL_SUCCESS;
}
/**************************************************
* FUNCTION NAME: dbDisconnect
*
* SYNOPSIS:
* terminate a data base connection
***************************************************/
int dbDisconnect(CONNECTION si)
{
int Status;
// QELib disconnect
Status = qeDisconnect(si->hdbc);
if (Status)
return Status;
else
return SQL_SUCCESS;
}
/**************************************************
* FUNCTION NAME: BuildConnectString
*
* SYNOPSIS: build connection string for DBF
***************************************************/
POINTER BuildConnectString(DATASRC dsrc)
{
char far *conec;
char *str="DRV=";
/* to connect to a DBF database, supply only
the driver parameter */
strcat(str,dsrc->DBDriver);
conec = (char far *) farmalloc(CONECLEN);
movedata(FP_SEG(str), FP_OFF(str),
FP_SEG(conec), FP_OFF(conec),
strlen(str));
return conec;
}
/**************************************************
* FUNCTION NAME: DecodeQEDataType
*
* SYNOPSIS: return description of data type
***************************************************/
char *DecodeQEDataType(int DataType)
{
static char Type[20];
strcpy(Type,"Unknown");
if (DataType == 1)
strcpy(Type,"character");
if (DataType == 2)
strcpy(Type,"variable len char");
if (DataType == 3)
strcpy(Type,"decimal");
if (DataType == 4)
strcpy(Type,"long integer");
if (DataType == 5)
strcpy(Type,"integer");
if (DataType == 6)
strcpy(Type,"float");
if (DataType == 7)
strcpy(Type,"double");
if (DataType == 8)
strcpy(Type,"date-time");
return Type;
}
/**************************************************
* FUNCTION NAME: FindNumberofColumns
*
* SYNOPSIS:
* get the number of columns in the request
***************************************************/
int FindNumberOfColumns( HSTMT hstmt )
{
int n;
n = qeNumCols (hstmt);
return n;
}